﻿var bool = auto_new = false;
var fp=0;
var un=0;
var ball_odds = cl_hao = cl_dx = cl_ds = cl_zhdx = cl_zhds = cl_lh ='';
var is_fenpan = true;
var min_money = 1;
var max_money = 99999999;


//限制只能输入1-9纯数字
function digitOnly ($this) {
    var n = $($this);
    var r = /^\+?[1-9][0-9]*$/;
    if (!r.test(n.val())) {
        n.val("");
    }
}
function reset(){
    document.orders.reset();
}

//10秒倒计时
function countdown(waittime){
    if(waittime<=0){
        clearTimeout(cd);
        waittime=10;
        updateOpenTime();
        auto(1);
        window.parent.account();
    }
    waittime--;
    cd=setTimeout("countdown("+waittime+")",1000);
    $("#countdown").html(waittime+"秒");
}


function updateOpenTime(){
    $.post("class/odds_cq.php", function(data){
        if(!data.isopen){
            clearTimeout(fp);
            clearTimeout(kj);

            $("#open_qihao").html("第 "+data.number+" 期");
            loadodds(data.oddslist);
            endtime(data.endtime);
            kjtime(data.opentime);
        }
    }, "json");
}
//盘口信息
function loadinfo(){
	$.post("class/odds_cq.php", function(data)
		{
            if(data.opentime>0){
                is_fenpan = false;
                if(!data.isopen){
                    is_fenpan = true;
                }
                $("#open_qihao").html("第 "+data.number+" 期");
                ball_odds = data.oddslist;
                loadodds(data.oddslist); //读取类型及赔率
                endtime(data.endtime);   //封盘时间
                kjtime(data.opentime);   //开奖时间
                auto(1);
                countdown(10);
                min_money = data.min_money;
                max_money = data.max_money;
            }else{
                is_fenpan = true;
                close_td();
                return false;
            }
		}, "json");
}

//更新赔率
function loadodds(oddslist){
    if (oddslist == null || oddslist == "" || is_fenpan) {
        close_td();
        return false;
    }
    //console.log(oddslist);
    for(var b = 1; b<=10; b++){
        if(b<=9){
            for(var s = 1; s<=14; s++){
                odds = oddslist.ball[b][s];
                $("#ball_"+b+"_h"+s).html(odds);
                loadinput(b , s);
            }
        }else{
            for(var s = 1; s<=15; s++){
                odds = oddslist.ball[b][s];
                $("#ball_"+b+"_h"+s).html(odds);
                loadinput(b , s);
            }
        }
    }
    $(".remove_gan").html("");
}


//更新投注框
function loadinput(ball , s){
    //console.log(s);
    b = "ball_"+ball+"_"+s;
    n = "<input name=\""+b+"\" id=\""+b+"\" class=\"inp1\" onkeyup=\"digitOnly(this)\" onfocus=\"this.className='inp1m'\" onblur=\"this.className='inp1';\" type=\"text\" maxLength=\"5\"/>"
    $("#ball_"+ball+"_t"+s).html(n);
}

//封盘时间
function endtime(iTime)
{
    var iMinute,iSecond;
    var sMinute="",sSecond="",sTime="";
    iMinute = parseInt((iTime/60)%60);
    if (iMinute == 0){
        sMinute = "00";
    }
    if (iMinute > 0 && iMinute < 10){
        sMinute = "0" + iMinute;
    }
    if (iMinute > 10){
        sMinute = iMinute.toString();
    }
    iSecond = parseInt(iTime%60);
    if (iSecond >= 0 && iSecond < 10 ){
        sSecond = "0" + iSecond;
    }
    if (iSecond >= 10 ){
        sSecond =iSecond.toString();
    }
    sTime= sMinute + sSecond;

    if(iTime<0){
        //将原框类数据隐藏及清空
        $(".mask",window.parent.document).hide();
        $("#confirm_result",window.parent.document).empty();

        clearTimeout(fp);
        is_fenpan = true;
        close_td();
    }else{
        iTime--;
        var t = 'time';
        $("#fptime").html(sTime.substr(0,2)+":"+sTime.substr(2,3));
        fp = setTimeout("endtime("+iTime+")",1000);
    }
}
//开奖时间
function kjtime(iTime)
{
    var iMinute,iSecond;
    var sMinute="",sSecond="";
    iMinute = parseInt((iTime/60)%60);
    if (iMinute == 0){
        sMinute = "00";
    }
    if (iMinute > 0 && iMinute < 10){
        sMinute = "0" + iMinute;
    }
    if (iMinute > 10){
        sMinute = iMinute.toString();
    }
    iSecond = parseInt(iTime%60);
    if (iSecond >= 0 && iSecond < 10 ){
        sSecond = "0" + iSecond;
    }
    if (iSecond >= 10 ){
        sSecond =iSecond.toString();
    }
    sTime= sMinute + sSecond;

    if(iTime<0){
        //新开盘时间清空上一期下注记录
        $(".order",window.parent.document).remove();
        $("#tzinfo",window.parent.document).hide();

        clearTimeout(cd);
        clearTimeout(kj);
        loadinfo();
    }else{
        iTime--;
        $("#kjtime").html(sTime.substr(0,2)+":"+sTime.substr(2,3));
        kj = setTimeout("kjtime("+iTime+")",1000);
    }
}


//更新开奖号码
function auto(ball) {
    $("#lotteryname",window.parent.document).html("重庆时时彩");
    $.post("class/auto_cq.php", {ball: ball}, function (data) {
        //console.log(data);
        //查找当前框架的父级框架并修改父级框架的元素
        $("#qihao",window.parent.document).html(data.numbers);
        //获取下一期的开奖期号
        var openqihao = $("#open_qihao").html();
        if (auto_new == false || openqihao - data.numbers == 1) {
            //循环拼接开奖结果html代码，并推送到父级框架指定位置显示
            var numinfo = '';
            for (var b = 0; b < 4; b++) {
                numinfo += '<div class="virtual">' + data.hms[b] + '</div>';
            }
            $("#result",window.parent.document).html(numinfo);
            //循环拼接开奖球号html代码，并推送到父级框架指定位置显示
            var qiuhao = '';
            for (var i = 0; i < data.hm.length; i++){
                qiuhao += '<b class="ball l' + data.hm[i] + '"></b>';
            }
            var result_balls_HTML='<li class="clear lose" id="qiuhao">'+qiuhao+'</li>';
            result_balls_HTML+='<li class="clear location" id="result">'+numinfo+'</li>';
            $("#result_balls",window.parent.document).html(result_balls_HTML);
        }
    }, "json");
}

//关闭赔率及金额
function close_td(){
    $(".bottom-brass input[type='text']").val("");
    $(".min-tab-td1").html("-");
    $(".max-tab-td1").html("-");
    $(".remove_gan").html("");
    $(".min-tab-td2").html("");
    $(".max-tab-td2").html("");
}


//读取第几球
function did (type)
{
	var r = '';
	switch (type)
	{
		case 1 : r = '第一球'; break;
		case 2 : r = '第二球'; break;
		case 3 : r = '第三球'; break;
		case 4 : r = '第四球'; break;
		case 5 : r = '第五球'; break;
		case 6 : r = '总和、龙虎和'; break;
		case 7 : r = '前三'; break;
		case 8 : r = '中三'; break;
		case 9 : r = '后三'; break;
		case 10 : r = '牛牛'; break;
	}
	return r;
}
//读取玩法
function wan (type)
{
	var r = '';
	switch (type)
	{
		case 1 : r = '0'; break;
		case 2 : r = '1'; break;
		case 3 : r = '2'; break;
		case 4 : r = '3'; break;
		case 5 : r = '4'; break;
		case 6 : r = '5'; break;
		case 7 : r = '6'; break;
		case 8 : r = '7'; break;
		case 9 : r = '8'; break;
		case 10 : r = '9'; break;
		case 11 : r = '大'; break;
		case 12 : r = '小'; break;
		case 13 : r = '单'; break;
		case 14 : r = '双'; break;
	}
	return r;
}
//读取玩法
function wan6 (type)
{
	var r = '';
	switch (type)
	{
		case 1 : r = '总和大'; break;
		case 2 : r = '总和小'; break;
		case 3 : r = '总和单'; break;
		case 4 : r = '总和双'; break;
		case 5 : r = '龙'; break;
		case 6 : r = '虎'; break;
		case 7 : r = '和'; break;
	}
	return r;
}
//读取玩法
function wan789 (type)
{
	var r = '';
	switch (type)
	{
		case 1 : r = '豹子'; break;
		case 2 : r = '顺子'; break;
		case 3 : r = '对子'; break;

		case 4 : r = '半顺'; break;
		case 5 : r = '杂六'; break;
	}
	return r;
}
//读取玩法
function wan10 (type)
{
    var r = '';
    switch (type)
    {
        case 1 : r = '无牛'; break;
        case 2 : r = '牛1'; break;
        case 3 : r = '牛2'; break;
        case 4 : r = '牛3'; break;
        case 5 : r = '牛4'; break;
        case 6 : r = '牛5'; break;
        case 7 : r = '牛6'; break;
        case 8 : r = '牛7'; break;
        case 9 : r = '牛8'; break;
        case 10 : r = '牛9'; break;
        case 11 : r = '牛牛'; break;
        case 12 : r = '牛大'; break;
        case 13 : r = '牛小'; break;
        case 14 : r = '牛单'; break;
        case 15 : r = '牛双'; break;
    }
    return r;
}

//开始加载
loadinfo(); //加载数据项

$(function () {
    $('.alert-mask').click(function () {

        var mix = min_money; cou = m = 0, txt = '', c=true;
        var max_true = true;

        var res = {};
        var json = [];
        var one_money;

        for (var i = 1; i < 11; i++){
            if(i==6){
                for(var s = 1; s < 8; s++){
                    if ($("#ball_" + i + "_" + s).val() != "" && $("#ball_" + i + "_" + s).val() != null) {

                        one_money = parseInt($("#ball_" + i + "_" + s).val());

                        m = m + one_money;
                        //获取投注项，赔率
                        var odds = $("#ball_" + i + "_h" + s).html();
                        var q = did(i);
                        var w = wan6(s);
                        res = {
                            odds: odds,
                            qiu: q,
                            wan: w,
                            one_money: one_money,
                            ball_id: "ball_" + i + "_" + s
                        };
                        json.push(res);
                        cou++;
                    }
                }
            }else if(i==7 || i==8 || i==9){
                for(var s = 1; s < 6; s++){
                    if ($("#ball_" + i + "_" + s).val() != "" && $("#ball_" + i + "_" + s).val() != null) {

                        one_money = parseInt($("#ball_" + i + "_" + s).val());

                        m = m + one_money;
                        //获取投注项，赔率
                        var odds = $("#ball_" + i + "_h" + s).html();
                        var q = did(i);
                        var w = wan789(s);
                        res = {
                            odds: odds,
                            qiu: q,
                            wan: w,
                            one_money: one_money,
                            ball_id: "ball_" + i + "_" + s
                        };
                        json.push(res);
                        cou++;
                    }
                }
            }else if(i==10){
                for(var s = 1; s < 16; s++){
                    if ($("#ball_" + i + "_" + s).val() != "" && $("#ball_" + i + "_" + s).val() != null) {

                        one_money = parseInt($("#ball_" + i + "_" + s).val());

                        m = m + one_money;
                        //获取投注项，赔率
                        var odds = $("#ball_" + i + "_h" + s).html();
                        var q = did(i);
                        var w = wan10(s);
                        res = {
                            odds: odds,
                            qiu: q,
                            wan: w,
                            one_money: one_money,
                            ball_id: "ball_" + i + "_" + s
                        };
                        json.push(res);
                        cou++;
                    }
                }
            }else{
                for(var s = 1; s < 15; s++){
                    if ($("#ball_" + i + "_" + s).val() != "" && $("#ball_" + i + "_" + s).val() != null) {
                        one_money = parseInt($("#ball_" + i + "_" + s).val());

                        m = m + one_money;
                        //获取投注项，赔率
                        var odds = $("#ball_" + i + "_h" + s).html();
                        var q = did(i);
                        var w = wan(s);
                        res = {
                            odds: odds,
                            qiu: q,
                            wan: w,
                            one_money: one_money,
                            ball_id: "ball_" + i + "_" + s
                        };
                        json.push(res);
                        cou++;
                    }
                }
            }
        }

        if (cou <= 0||is_fenpan){
            $("#consult",window.parent.document).html('当前已封盘或输入的金额不正确，请重新选择！');
            $(".mask-two",window.parent.document).show();
        } else {
            $("#confirm_result",window.parent.document).append('<tr><th>号码</th><th>赔率</th><th>金额</th><th>确认</th></tr>');
            //循环遍历展示数据
            for (var i = 0; i < cou; i++) {
                $("#confirm_result",window.parent.document).append('<tr class="lines"><th>' + '<label>' + json[i]['qiu'] + '</label>&nbsp;<span>' + json[i]['wan'] + '</span></th><th><dt>' + json[i]['odds'] + '</dt></th><th><input type="text" id=' + json[i]['ball_id'] + ' name=' + json[i]['ball_id'] + ' value="' + json[i]['one_money'] + '"   maxlength="5" readonly></th><th><input type="checkbox" class="my_check" checked></th></tr>');
            }
            $("#confirm_result",window.parent.document).append('<tr><th colspan="2" >数组:' + '<span id="cou">' + cou + '</span></th><th colspan="2">总金额：' + '<span id="total_money">' + m + '</span></th></tr>');
            $(".mask",window.parent.document).show();

            //修改输入框时总金额随之改变
            $("input[type=text]",window.parent.document).change(function () {
                var value1 = 0;
                $("input[type=text]",window.parent.document).each(function () {

                    value1 += $(this).val() * 1;//框的值
                })
                $(this).parent().parent().siblings().find('#total_money').text(value1);//需要被修改的值
            })


            //点击删除复选框
            $("input[type=checkbox]",window.parent.document).click(function () {

                var value1 = $(this).parent().parent().siblings().find('#total_money').text() * 1;//总金额
                var value2 = $(this).parent().siblings().find('input[type=text]').val();
                var value3 = value1 - value2;
                $(this).parent().parent().siblings().find('#total_money').text(value3);//需要被修改的值
                value2 = $(this).parent().parent().siblings().find('#cou').text() * 1 - 1;
                $(this).parent().parent().siblings().find('#cou').text(value2);//数组值
                //移除选中的内容
                $(this).parent().parent().remove();

                if (value2 == '0') {
                    $('.close', window.parent.document).trigger("click");
                }

            });
            //将当前页需要传值的页面赋值给父框架
            $("#orders",window.parent.document).attr("action", "class/order_cq_ssc.php");
        }
        $('.reset').trigger("click");
    });

    //进入页面获取默认数据
    get_cqsscresult(1);

    //获取后台对应的数据
    function get_cqsscresult(re) {

        $.get("class/get_cqsscresult.php?ball=" + re, function (data) {
            var html = "";
            //console.log(data);
            for (var i = 0; i < 10; i++) {
                html += "<td>" + data[i] + "</td>";
            }
            $("#result").html(html);
        }, "json");
    }

    //点击按钮样式及数据改变
    $('#cg tr td').click(function () {

        if (!$(this).is(".bjs")) {

            $(this).addClass("bjs");
            $(this).siblings().removeClass("bjs");
            var index = $('#cg tr td').index(this);
            get_cqsscresult(index + 1);
        }

    });


    //给冠亚军/龙虎添加字体颜色
    function add_color(i) {

        if (i == "大" || i == "双" || i == "龙") {

            return '<span style="color:red">' + i + '</span>';
        } else {

            return '<span style="color:black">' + i + '</span>';
        }
    }

    //获取后台走势对应数据(号码)
    function get_cqssctrend(val) {

        $.get("./class/get_cqssctrend.php?val=" + val, function (data) {

            //console.log(val);
            if(val==1){//获取后台走势对应数据(号码)

                for (var j = 0; j < data.length; j++) {

                    $(window.parent.document).contents().find('#trend').append(
                        '<tr><th>' + data[j].qishu + '</th><th>' + data[j].datetime +
                        '</th><td><b class="ball l' + data[j].ball_1 + '"></b></td><td>' +
                        '</td><td><b class="ball l' + data[j].ball_2 + '"></b></td><td>' +
                        '</td><td><b class="ball l' + data[j].ball_3 + '"></b></td><td>' +
                        '</td><td><b class="ball l' + data[j].ball_4 + '"></b></td><td>' +
                        '</td><td><b class="ball l' + data[j].ball_5 + '"></b></td><td>' +
                        '</tr>');
                }

            } else if(val==2||val==3){//获取后台走势对应数据(单双/大小)

                for (var j = 0; j < data.length; j++) {

                    $(window.parent.document).contents().find('#trend').append(
                        '<tr><th>' + data[j].qishu + '</th><th>' + data[j].datetime +
                        '</th><td><b class="dxds b' + data[j].ball_1 + '"></b></td><td>' +
                        '</td><td><b class="dxds b' + data[j].ball_2 + '"></b></td><td>' +
                        '</td><td><b class="dxds b' + data[j].ball_3 + '"></b></td><td>' +
                        '</td><td><b class="dxds b' + data[j].ball_4 + '"></b></td><td>' +
                        '</td><td><b class="dxds b' + data[j].ball_5 + '"></b></td><td>' +
                        '</tr>');
                }
            } else if(val==4){//获取后台走势对应数据(冠亚军/龙虎)

                for (var j = 0; j < data.length; j++) {
                       // console.log(data);
                    $(window.parent.document).contents().find('#trend').append(
                        '<tr><th>' + data[j][0] + '</th><th>' + data[j][1] +
                        '</th><td>' + add_color(data[j][2]) + '</td><td>' +
                        '</td><td><b class="dxds b' + data[j][3] + '"></b></td><td>' +
                        '</td><td><b class="dxds b' + data[j][4] + '"></b></td><td>' +
                        '</td><td>' + add_color(data[j][5]) + '</td><td>' +
                        '</tr>');
                }

            }

        }, "json");
    }

    //结果走势弹出框
    $('.jgzs').click(function () {
        //将对应的标题数据赋值给父框架
        $(".bt",window.parent.document).find('span').eq(0).text('重庆时时彩 - 结果走势');
        $(".item",window.parent.document).find('a').eq(0).text('号码');
        $(".item",window.parent.document).find('a').eq(1).text('大小');
        $(".item",window.parent.document).find('a').eq(2).text('单双');
        $(".item",window.parent.document).find('a').eq(3).text('总和/龙虎');
        //获取初始默认数据
        get_cqssctrend(1);
        //点击按钮样式及数据改变
        $(".item",window.parent.document).find('a').click(function () {
            //获取点击数据对应编号
            var index = $(".item",window.parent.document).find('a').index(this);

            $("#trend",window.parent.document).find('tr').remove();

            get_cqssctrend(index+1);

        });
        $(".results-the-trend",window.parent.document).show();
    });

});
